#!/home/berald01/.local/bin/python

import argparse
import os
import re
import sys

MARKER= '------------------------------------------------------------\n\n' ## Scan each log file in search of this string. The line after this will be returned

parser = argparse.ArgumentParser(description= """

DESCRIPTION:
                                    
   Read log files produced by lsf/bsub and returns the exit message.
   
   log_reader.py scans the log files to search for the string(s) of 60 dashes and returns
   the line after the blank one. E.g.:

...
------------------------------------------------------------

Exited with exit code 127.  <--- This line returned
...

OUTPUT:
    tab-separated lines (one per file) with
    1. File name
    2nd to second last: Exit messages (Note: with default only the last message is returned)
    Last: Number of exit messages found

EXAMPLES:
    # All files ending in 'out.log':
    log_reader.py -r 'out\.log$'
    
    # Get only unsuccssefull jobs:
    log_reader.py | grep -v Successfully

TODO:
""", formatter_class= argparse.RawTextHelpFormatter)

parser.add_argument('-r', '--regex', # nargs='?', default=sys.stdout,
                    type= str,
                    required= False,
                    default= '\.log$',
                    help="""Scan all files matching this regex. Default is '\.log$'
(all files ending in .log)

                    """)

parser.add_argument('-d', '--dir', # nargs='?', default=sys.stdout,
                    type= str,
                    default= '.',
                    help="""Directory to search. Default is current dir
                    
                    """)

parser.add_argument('-a', '--all', # nargs='?', default=sys.stdout,
                    action= 'store_true',
                    help="""With this flag all the exit statuses are returned. Without it (default)
only the last status is returned
                    
                    """)

args = parser.parse_args()

# Get log files
logs= sorted(os.listdir(args.dir))
logs= [os.path.join(args.dir, x) for x in logs if re.search(args.regex, x)]

p= re.compile(MARKER)
for lfile in logs:
    lstr= open(lfile).read()
    iterator= p.finditer(lstr)
    exit_lst= [lfile]
    for m in iterator:
        exit_start= m.end()  ## Where the line to return starts
        exit_end= exit_start + re.search('\n', lstr[exit_start:]).start()  ## Get the end of lin
        exit_lst.append(lstr[exit_start:exit_end])
    nstats= str(len(exit_lst)-1)
    if not args.all:
        "Return only the last status"
        exit_lst= [exit_lst[0]] + [exit_lst[-1]]
    exit_lst.append(nstats)  ## Number of matches found
    exit_status= '\t'.join(exit_lst)
    print(exit_status)